<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class NationFlag extends Base
{
    protected $table = 'nation_flag_log';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function create($user_id)
    {
        $data['user_id'] = $user_id;
        return DB::table($this->table)->insert($data);
    }

    public function getFlag($user_id)
    {
        $where['user_id'] = $user_id;
        $res = DB::table($this->table)->where($where)->first();
        if (!$res) {
            return false;
        }
        return $res;

    }

    public function updateInviteNum($invite_uid)
    {
        $where['user_id'] = $invite_uid;
        return DB::table($this->table)->where($where)->increment('invite_num', 1);
    }


    public function updateRabbit($invite_uid)
    {
        $where['user_id'] = $invite_uid;
        DB::table($this->table)->where($where)->increment('real_num', 1);
        return DB::table($this->table)->where($where)->increment('rabbit_num', 1);
    }


    public function getUserCp($user_id)
    {
        $where['user_id'] = $user_id;
        $data = DB::connection("mysql_kkaudio")
            ->table("coupling")
            ->where($where)
            ->select(['user_id', 'cp_user_id'])->first();
        return self::objToArray($data);
    }

    //获取用户喜鹊数量
    public function getRabbitNum($w_arr = [])
    {
        return DB::table($this->table)
            ->whereIn('user_id', $w_arr)
            ->select(['user_id', 'fowl_num', 'consumer_card_num', 'consumer_intimacy_num', 'real_num'])->get()->toArray();
    }

    //消费喜鹊
    public function updateRabbitToConvert($user_id, $intimacy, $type = 1)
    {
        $where['user_id'] = $user_id;
        switch ($type) {
            case 1:
                DB::table($this->table)->where($where)->increment('consumer_intimacy_num', $intimacy);
                break;
            case 2:
                DB::table($this->table)->where($where)->increment('consumer_card_num', $intimacy);
                break;
            default:
        }
        return DB::table($this->table)->where($where)->decrement('real_num', $intimacy);
    }

    public function updateStatus($user_id)
    {
        $where['user_id'] = $user_id;
        $where['status'] = 1;
        $data['is_draw'] = 1;
        return DB::table($this->table)->where($where)->update($data);
    }

    public function updateUserFlag($user_id, $flag)
    {
        $where['user_id'] = $user_id;
        DB::table($this->table)->where($where)->increment('flag_num', $flag);
        return DB::table($this->table)->where($where)->increment('real_num', $flag);
    }


}